<?php

/**
 * 资讯 API
 * User: don
 */

namespace app\modules\app\controllers;

use yii;
use app\libs\AppControl;
use app\libs\Method;
use app\libs\Pager;
use app\libs\VerificationCode;
use app\libs\Sms;
use app\modules\content\models\Info;
use app\modules\content\models\Teachers;

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');

class InfoController extends AppControl
{

    public $enableCsrfValidation = false;

    function init (){
        parent::init();
        include_once($_SERVER['DOCUMENT_ROOT'].'/../libs/ucenter/ucenter.php');
    }

    /**
     * [actionIeltsInfo 雅思资讯]
     * @Author don
     * @Date   2021-10-26
     * @return [type]     [description]
     */
    public function actionList()
    {
        $cate = Yii::$app->request->post('cate', 1);  // 1最新资讯 2干货分享 3申友头条 4考位资讯
        $page = Yii::$app->request->post('page', 1);  // 页码
        $pageSize = Yii::$app->request->post('pageSize', 10);  // 每页条数
        $offset = $pageSize * ($page - 1);
        $time = time();
        $data = Info::find()->asArray()->where("(pushTime<=$time or pushTime is null) and cate=" . $cate)->offset($offset)->limit($pageSize)->orderBy('id desc')->all();
        if ($data) {
            foreach ($data as $key => $value) {
                $data[$key]['image'] = "https://www.thinkuprep.com".$value['pic'];
                $data[$key]['createTime'] = $value['pushTime'] ? date('Y-m-d', $value['pushTime']) : date('Y-m-d', $value['createTime']);
            }
        }
        die(json_encode(['code'=>1, 'data'=>$data]));
    }
    
    /**
     * [actionIeltsInfoDetail 雅思资讯详情]
     * @Author don
     * @Date   2021-10-26
     * @return [type]     [description]
     */
    public function actionDetail()
    {
        $id = Yii::$app->request->post('id');
        $data = Info::find()->asArray()->where('id='.$id)->one();
        if (!$data) {
            die(json_encode(['code'=>0, 'message'=>'未找到资讯详情！']));
        }
        $data['content'] = str_replace("/files/", "http://www.thinkuprep.com/files/",$data['content']);

        Info::updateAll(['viewCount' => ($data['viewCount'] + 1)], "id=$id");
        $data['image'] = "https://www.thinkuprep.com".$data['pic'];
        $data['createTime'] = $data['pushTime'] ? date('Y-m-d', $data['pushTime']) : date('Y-m-d', $data['createTime']);

        $time = time();
        if ($data['cate'] == 3 ) {
            $recommend = Info::find()->asArray()->where('cate=3 and pushTime<='.$time)->orderBy('viewCount desc')->limit(4)->all();
        } else {
            $recommend = Info::find()->asArray()->where('(cate=1 or cate=2) and pushTime<='.$time)->orderBy('sort asc,id desc')->limit(4)->all();
        }
        
        if ($recommend) {
            foreach ($recommend as $key => $value) {
                $recommend[$key]['image'] = "https://www.thinkuprep.com".$value['pic'];
                $recommend[$key]['createTime'] = $value['pushTime'] ? date('Y-m-d', $value['pushTime']) : date('Y-m-d', $value['createTime']);
            }
        }

        die(json_encode(['code'=>1, 'data'=>$data, 'recommend'=>$recommend]));
    }
}
